package com.caishi.lkx.exam.model;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.caishi.lkx.exam.records.dto.QuestionRecordsDto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzw.common.entity.impl.BSDEntity;
import com.zzw.common.service.model.NameModel;
import com.caishi.lkx.exam.QuestionCollection;
import com.caishi.lkx.exam.QuestionCollectionBlockItem;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Schema(title = "每日一练")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "ex_every_day_practice", autoResultMap = true)
public class EveryDayPracticeModel extends BSDEntity<EveryDayPracticeModel> implements NameModel<EveryDayPracticeModel, String>, QuestionCollection<EveryDayPracticeModel, String> {

    /**
     * 所属科目id
     */
    private Long categoryId;

    private LocalDate day;

    private String userId;

    private Integer qcount;

    private Integer continuityDay;

    private String name;

    @TableField(value = "content", typeHandler = JacksonTypeHandler.class)
    @Schema(title = "试题内容")
    @JsonIgnore
    private List<Map<String, Object>> contentMap;

    public List<QuestionCollectionBlockItem> getContent() {
        if (contentMap != null) {
            return contentMap.stream().map(v -> BeanUtil.fillBeanWithMap(v, new QuestionCollectionBlockItem(), true)).toList();
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public void setContent(List<QuestionCollectionBlockItem> content) {
        if (content == null) {
            this.contentMap = null;
            return;
        }
        this.contentMap = content.stream().map(BeanUtil::beanToMap).toList();
    }


    @TableField(exist = false)
    private Integer joinCount;


    @TableField(exist = false)
    @Schema(title = "试卷做题记录信息")
    private QuestionRecordsDto records;
}
